package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //1、新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        this.setmealMapper.save(setmeal);

        //2、新增套餐下的菜品
        if(!CollectionUtils.isEmpty(setmealDTO.getSetmealDishes())){
            final List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            setmealDishes.forEach(sd -> sd.setSetmealId(setmeal.getId()));
            this.setmealDishMapper.batchSave(setmealDishes);
        }

    }

    /**
     * 分页查询
     *
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        Page<SetmealVO> page = this.setmealMapper.pageQuery(pageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        //1、判断是否有起售中的套餐
        Integer count = this.setmealMapper.countByIdsAndStatus(ids);
        if(count != 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //2、删除套餐内容
        this.setmealMapper.batchDelete(ids);

        //3、删除套餐关联数据
        this.setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 根据ID查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryById(Long id) {
        SetmealVO setmealVO = this.setmealMapper.queryById(id);
        return setmealVO;
    }

    /**
     * 更新套餐
     *
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //1、更新套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        this.setmealMapper.update(setmeal);

        if(!CollectionUtils.isEmpty(setmealDTO.getSetmealDishes())){
            //2、删除套餐关联菜品信息
            this.setmealDishMapper.deleteBySetmealId(setmeal.getId());

            //3、批量新增套餐关联的菜品信息
            final List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            setmealDishes.forEach(sd -> sd.setSetmealId(setmeal.getId()));
            this.setmealDishMapper.batchSave(setmealDishes);
        }
    }

    /**
     * 启售停售
     *
     * @param id
     * @param status
     */
    @Override
    public void startOrStop(Long id, Integer status) {
        //1、判断套餐内是否有停售的菜品，如果有，给出无法启售套餐的提示
        if(StatusConstant.ENABLE.equals(status)){
            Integer count = this.setmealMapper.countBySetmealIdandStatus(id);
            if(count != 0 ){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        //2、更新状态
        final Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        this.setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
